package gui;

import conn.ConnectionData;
import conn.SocketManager;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Nuova finestra per i dati di connessione.
 *
 * @author  Claudio "Dna" Bonesana
 */
public class ClientConnect extends JFrame{

    private JTextField tUsername;
    private JTextField tServer;
    private JTextField tPort;

    private JTextPane textPane;

    private SocketManager socketManager;

    /**
     * @param socketManager un {@link SocketManager} valido
     */
    public ClientConnect(SocketManager socketManager, Component clientWindow) {

        this.socketManager = socketManager;

        setTitle("Connect to a Server");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        tUsername = new JTextField(15);
        tServer = new JTextField(15);
        tPort = new JTextField(15);

        tUsername.setText("Username");
        tServer.setText("127.0.0.1");
        tPort.setText("4700");

        tUsername.addActionListener(new Connect());
        tServer.addActionListener(new Connect());
        tPort.addActionListener(new Connect());

        JLabel lName = new JLabel("Your username:");
        JLabel lServer = new JLabel("Server address:");
        JLabel lPort = new JLabel("Server port:");

        textPane = new JTextPane();

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,2,5,5));
        panel.add(lName);
        panel.add(tUsername);
        panel.add(lServer);
        panel.add(tServer);
        panel.add(lPort);
        panel.add(tPort);

        JButton connect = new JButton("Connect");
        connect.addActionListener(new Connect());

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.NORTH);
        getContentPane().add(textPane, BorderLayout.CENTER);
        getContentPane().add(connect, BorderLayout.SOUTH);

        pack();

        setLocationRelativeTo(clientWindow);
        setResizable(false);
    }

    /**
     * {@link ActionListener} utilizzato per recuperare i dati, creare un {@link ConnectionData} e avviare la connessione
     * attraverso il {@link SocketManager}
     */
    private class Connect implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            ConnectionData connectionData = new ConnectionData(tUsername.getText(),tServer.getText(),tPort.getText());

            if (connectionData.check()){            /*  Controllo superato  */
                dispose();
                socketManager.setConnectionData(connectionData);
                socketManager.connect();
            } else {                                /*  Controllo non superato */
                StyledDocument doc = textPane.getStyledDocument();

                Style style = textPane.addStyle("Style",null);
                StyleConstants.setForeground(style, Color.red);

                try {
                    doc.remove(0,doc.getLength());
                    doc.insertString(0, "Invalid parameters.", style);
                } catch (BadLocationException ignored) {}
            }
        }
    }
}
